package com.bing.controller.Login;

import com.bing.dto.StudentDto;
import com.bing.entity.Bp.BpInfo;
import com.bing.entity.Student;
import com.bing.mapper.StudentMapper;
import com.bing.response.Result;
import com.bing.service.StudentService;
import com.bing.service.UserService;
import com.bing.utils.idgenertor.IdGen;
import com.bing.web.BaseController;
import com.bing.web.BaseResponse;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 测试controller
 *
 * @author 胡日亮
 * @create 2018-7-15 10:00
 */
@RestController
@RequestMapping("/login")
@Component
@Aspect
@CrossOrigin
public class LoginController extends BaseController {
  @Autowired
  UserService userService;
  /*
  固定每一页显示数据条数qwer
   */
  private int pageSize = 5;


/**
   * @Description 登录 后台登陆
   * @Author 胡日亮
   * @Date 2019/09/04 16:01
   */

  @RequestMapping("/login")
  @ResponseBody
  public Result login(String user, String password, HttpSession httpSession) {
    int result = userService.findUser(user);
    if (result == 0) {
      return new Result(13000, "账号不存在");
    }
    result = userService.validate(user, password);
    if (result > 0) {
      httpSession.setAttribute("login", user);
    }
    return result == 0 ? new Result(13000, "密码错误") : Result.SUCCESS;
  }


  @GetMapping("/index")
  public String index() {
    return "/login";
  }

  /**
   * @Description 退出登录
   * @Author 胡日亮
   * @Date 2017/12/21 10:18
   */
  @RequestMapping("/exit")
  public String exit(HttpSession httpSession) {
    httpSession.removeAttribute("login");
    return "redirect:/student/index";
  }



  /**
   * @Description 退出登录
   * @Author 胡日亮
   * @Date 2017/12/21 10:18
   */
  @RequestMapping("/bpexit")
  public ResponseEntity<BaseResponse> bpexit(String tel,HttpSession httpSession) {
    httpSession.removeAttribute(tel);
    return buildSuccessResponse();
  }


  @ApiOperation(value = "用户列表")
  @RequestMapping(value = "getBpList")
  public ResponseEntity<BaseResponse> getBpinfo(@RequestParam(defaultValue = "1") Integer pageNum) throws Exception{
    return buildSuccessResponse(userService.getBpinfoList(pageNum));
  }



}